﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Fayshine.VA.Helpers;
using Fayshine.VA.Models;
using Fayshine.VA.RIAService.Web;
using Fayshine.VA.ViewModels;
using Visifire.Charts;
using LocalBusyIndicator = Fayshine.VA.Controls.BusyIndicator;

namespace Fayshine.VA.Views.SearchStatistics.Month
{
    public partial class MonthWeekStatistics : Page
    {
        private StatisticsReportViewModel m_ViewModel;
        private string m_TimeTitle = string.Empty;
        public MonthWeekStatistics()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MonthWeekStatistics_Loaded);
        }

        private void MonthWeekStatistics_Loaded(object sender, RoutedEventArgs e)
        {
            LocalBusyIndicator indicator = Application.Current.RootVisual as LocalBusyIndicator;
            indicator.IsBusy = true;

            cmbYear.ItemsSource = FayshineManager.Instance.GetYearSource();
            cmbYear.SelectedValue = DateTime.Now.Year;

            cmbMonth.ItemsSource = FayshineManager.Instance.GetMonthSource();
            cmbMonth.SelectedValue = DateTime.Now.Month;

            m_ViewModel = new StatisticsReportViewModel();
            m_ViewModel.ResetHisotryChartEvent += new ResetHistoryChartEventHandler(m_ViewModel_ResetHisotryChartEvent);
            m_ViewModel.AppendHistoryPassengerFlow += new AppendHistoryPassengerFlowEventHandler(m_ViewModel_AppendHistoryPassengerFlow);
            this.DataContext = m_ViewModel;

            m_ViewModel.GetAllAreas();
        }

        void m_ViewModel_AppendHistoryPassengerFlow(string title, bool isIn, List<HistoryPassengerFlow> firstHistoryPassengerFlow, List<HistoryPassengerFlow> secondHistoryPassengerFlow)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                List<DayOfWeek> dayOfWeeks = new List<DayOfWeek>();
                DateTime time = new DateTime((int)cmbYear.SelectedValue, (int)cmbMonth.SelectedValue, 1, 0, 0, 0);
                DateTime endTime = time.AddMonths(1);

                var workDaySource = GetSelectStatisticsSource();

                Axis xAxis = new Axis();
                DateTime minDateTime = workDaySource.Select(s => s.HistoryTime).Min();
                DateTime maxDateTime = workDaySource.Select(s => s.HistoryTime).Max();
                xAxis.AxisMinimum = minDateTime;
                xAxis.AxisMaximum = maxDateTime;
                if (maxDateTime.Year == minDateTime.Year && maxDateTime.Month == minDateTime.Month &&
                    maxDateTime.Day == minDateTime.Day)
                {
                    xAxis.ValueFormatString = "HH:mm";
                    xAxis.IntervalType = IntervalTypes.Hours;
                    xAxis.Interval = 1;
                }
                else
                {
                    xAxis.IntervalType = IntervalTypes.Hours;
                    xAxis.Interval = 4;
                }
                vcChart1.AxesX.Add(xAxis);

                string title1 = string.Empty;

                var selectAreaExts = m_ViewModel.AreaExtends.Where(a => a.IsSelected).ToList();
                if (selectAreaExts.Count == 1)
                {
                    title1 = selectAreaExts[0].Name + " 各通道 ";
                }
                else if (selectAreaExts.Count > 1)
                {
                    title1 = " 各店铺 ";
                }
                string inOrOut = cmbInorOut.SelectedIndex == 0 ? "进" : "出";
                ComboBoxItem item = cmbWeek.SelectedItem as ComboBoxItem;
                string timeTitle = (string)item.Content;
                Title vcChart1Title = new Visifire.Charts.Title();
                vcChart1Title.Text = time.Year + "年" + time.Month + "月" + title1 + " " + timeTitle + " 每日平均客流分布（" + inOrOut + ")";
                vcChart1.Titles.Add(vcChart1Title);

                this.dataGridLabel.Text = time.Year + "年" + time.Month + "月" + title1 + " 周一至周日 每日平均客流分布（" + inOrOut + ")";
                if (cmbWeek.SelectedIndex == 0)
                {
                    var mondaySeries = GetWorkDayDataSeries(time, endTime, DayOfWeek.Monday);
                    var tuesdaySeries = GetWorkDayDataSeries(time, endTime, DayOfWeek.Tuesday);
                    var wedSeries = GetWorkDayDataSeries(time, endTime, DayOfWeek.Wednesday);
                    var thursSeries = GetWorkDayDataSeries(time, endTime, DayOfWeek.Thursday);
                    var fridaySeries = GetWorkDayDataSeries(time, endTime, DayOfWeek.Friday);
                    var satSeries = GetWorkDayDataSeries(time, endTime, DayOfWeek.Saturday);
                    var sunSeries = GetWorkDayDataSeries(time, endTime, DayOfWeek.Sunday);
                    vcChart1.Series.Add(mondaySeries);
                    vcChart1.Series.Add(tuesdaySeries);
                    vcChart1.Series.Add(wedSeries);
                    vcChart1.Series.Add(thursSeries);
                    vcChart1.Series.Add(fridaySeries);
                    vcChart1.Series.Add(satSeries);
                    vcChart1.Series.Add(sunSeries);
                }
                else if (cmbWeek.SelectedIndex == 1)
                {
                    var mondaySeries = GetWorkDayDataSeries(time, endTime, DayOfWeek.Monday);
                    var tuesdaySeries = GetWorkDayDataSeries(time, endTime, DayOfWeek.Tuesday);
                    var wedSeries = GetWorkDayDataSeries(time, endTime, DayOfWeek.Wednesday);
                    var thursSeries = GetWorkDayDataSeries(time, endTime, DayOfWeek.Thursday);
                    var fridaySeries = GetWorkDayDataSeries(time, endTime, DayOfWeek.Friday);
                    vcChart1.Series.Add(mondaySeries);
                    vcChart1.Series.Add(tuesdaySeries);
                    vcChart1.Series.Add(wedSeries);
                    vcChart1.Series.Add(thursSeries);
                    vcChart1.Series.Add(fridaySeries);
                }
                else if (cmbWeek.SelectedIndex == 2)
                {
                    var satSeries = GetWorkDayDataSeries(time, endTime, DayOfWeek.Saturday);
                    var sunSeries = GetWorkDayDataSeries(time, endTime, DayOfWeek.Sunday);
                    vcChart1.Series.Add(satSeries);
                    vcChart1.Series.Add(sunSeries);
                }
                else
                {
                    DayOfWeek selectWeek = DayOfWeek.Monday;
                    if (cmbWeek.SelectedIndex == 3) selectWeek = DayOfWeek.Monday;
                    if (cmbWeek.SelectedIndex == 4) selectWeek = DayOfWeek.Tuesday;
                    if (cmbWeek.SelectedIndex == 5) selectWeek = DayOfWeek.Wednesday;
                    if (cmbWeek.SelectedIndex == 6) selectWeek = DayOfWeek.Thursday;
                    if (cmbWeek.SelectedIndex == 7) selectWeek = DayOfWeek.Friday;
                    if (cmbWeek.SelectedIndex == 8) selectWeek = DayOfWeek.Saturday;
                    if (cmbWeek.SelectedIndex == 9) selectWeek = DayOfWeek.Sunday;
  
                    var selectSeries = GetWorkDayDataSeries(time, endTime, selectWeek);
                    vcChart1.Series.Add(selectSeries);

                }

                this.dataGrid.ItemsSource = GetWeekComparePassengerFlow();
                

            });
        }

        private void m_ViewModel_ResetHisotryChartEvent()
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                if (vcChart1 != null)
                {
                    vcChart1.AxesX.Clear();
                    vcChart1.TrendLines.Clear();
                    vcChart1.Series.Clear();
                    vcChart1.Titles.Clear();
                }
            });
        }

        private void cmbAreas_DropDownClosed(object sender, EventArgs e)
        {
            if (m_ViewModel != null)
            {
                List<area> selectedArea = new List<area>();
                var selectAreaExt = m_ViewModel.AreaExtends.Where(a => a.IsSelected).ToList();
                selectAreaExt.ForEach(b =>
                {
                    area temp = b.Tag as area;
                    selectedArea.Add(temp);
                });

                if (selectedArea.Count > 0)
                {
                    cbItemDisplay.Content = selectedArea.Count > 1 ? selectedArea[0].Name + "..." : selectedArea[0].Name;

                    if (selectedArea.Count > 1)
                    {
                        this.cmbPassenger.IsEnabled = false;
                    }
                    else
                    {
                        this.cmbPassenger.IsEnabled = true;
                    }

                }
                else
                {
                    cbItemDisplay.Content = string.Empty;
                }
                cmbAreas.SelectedItem = null;
                cmbAreas.SelectedItem = cbItemDisplay;

                m_ViewModel.SelectAreasChanged(selectedArea);
                cmbPassenger_DropDownClosed(null, null);
            }
        }

        private void cmbPassenger_DropDownClosed(object sender, EventArgs e)
        {
            if (m_ViewModel != null)
            {
                var selectMonitorAreaExts = m_ViewModel.MonitorAreaGateExts.Where(a => a.IsSelected).ToList();

                if (selectMonitorAreaExts.Count > 0)
                {
                    cbItemDisplay2.Content = selectMonitorAreaExts.Count > 1 ? selectMonitorAreaExts[0].Name + "..." : selectMonitorAreaExts[0].Name;

                }
                else
                {
                    cbItemDisplay2.Content = string.Empty;
                }
                cmbPassenger.SelectedItem = null;
                cmbPassenger.SelectedItem = cbItemDisplay2;
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            string msg = string.Empty;
            
            m_ViewModel.UpdateCheckedAreas();

            if (!FayshineManager.Instance.ValidateAreaGateInputs(m_ViewModel.CheckedAreas,
                m_ViewModel.CheckedGates, ref msg))
            {
                MessageBox.Show(msg);
            }
            else
            {
                DateTime dateTime = new DateTime((int)cmbYear.SelectedValue, (int)cmbMonth.SelectedValue, 1);

                m_ViewModel.RefreshMonthWeekReport(true, dateTime);
            }
        }

        private List<HistoryPassengerFlow> GetSelectStatisticsSource()
        {
            List<DayOfWeek> dayOfWeeks = new List<DayOfWeek>();
            DateTime time = new DateTime((int)cmbYear.SelectedValue, (int)cmbMonth.SelectedValue, 1, 0, 0, 0);
            DateTime endTime = time.AddMonths(1);

            if (cmbWeek.SelectedIndex == 0)
            {
                dayOfWeeks.Add(DayOfWeek.Monday);
                dayOfWeeks.Add(DayOfWeek.Tuesday);
                dayOfWeeks.Add(DayOfWeek.Wednesday);
                dayOfWeeks.Add(DayOfWeek.Thursday);
                dayOfWeeks.Add(DayOfWeek.Friday);
                dayOfWeeks.Add(DayOfWeek.Saturday);
                dayOfWeeks.Add(DayOfWeek.Sunday);
            }
            else if (cmbWeek.SelectedIndex == 1)
            {
                dayOfWeeks.Add(DayOfWeek.Monday);
                dayOfWeeks.Add(DayOfWeek.Tuesday);
                dayOfWeeks.Add(DayOfWeek.Wednesday);
                dayOfWeeks.Add(DayOfWeek.Thursday);
                dayOfWeeks.Add(DayOfWeek.Friday);
            }
            else if (cmbWeek.SelectedIndex == 2)
            {
                dayOfWeeks.Add(DayOfWeek.Saturday);
                dayOfWeeks.Add(DayOfWeek.Sunday);
            }
            else
            {
                if (cmbWeek.SelectedIndex == 3) dayOfWeeks.Add(DayOfWeek.Monday);
                if (cmbWeek.SelectedIndex == 4) dayOfWeeks.Add(DayOfWeek.Tuesday);
                if (cmbWeek.SelectedIndex == 5) dayOfWeeks.Add(DayOfWeek.Wednesday);
                if (cmbWeek.SelectedIndex == 6) dayOfWeeks.Add(DayOfWeek.Thursday);
                if (cmbWeek.SelectedIndex == 7) dayOfWeeks.Add(DayOfWeek.Friday);
                if (cmbWeek.SelectedIndex == 8) dayOfWeeks.Add(DayOfWeek.Saturday);
                if (cmbWeek.SelectedIndex == 9) dayOfWeeks.Add(DayOfWeek.Sunday);
            }

            var source = m_ViewModel.GetHistoryPassengerByWeek(false, time, endTime, dayOfWeeks);


            return source;
        }

        private DataSeries GetWorkDayDataSeries(DateTime startTime, DateTime endTime, DayOfWeek dayOfWeek)
        {
            List<DayOfWeek> dayOfWeeks = new List<DayOfWeek>();
            dayOfWeeks.Add(dayOfWeek);
            var workDaySource = m_ViewModel.GetHistoryPassengerByWeek(false, startTime, endTime, dayOfWeeks);

            DataSeries series = ChartHelper.GetDataSeries(RenderAs.Line, GetWeekDayString(dayOfWeek), ChartValueTypes.Time);
            series.DataMappings.Add(ChartHelper.GetDataMapping("XValue", "HistoryTime"));
            if (cmbInorOut.SelectedIndex == 0)
            {
                series.DataMappings.Add(ChartHelper.GetDataMapping("YValue", "InCount"));
            }
            else
            {
                series.DataMappings.Add(ChartHelper.GetDataMapping("YValue", "OutCount"));
            }
            series.DataSource = workDaySource;

            return series;
        }

        private string GetWeekDayString(DayOfWeek dayOfWeek)
        {
            string weekDay = "星期一";
            switch (dayOfWeek)
            {
                case DayOfWeek.Sunday:
                    weekDay = "星期日";
                    break;
                case DayOfWeek.Monday:
                    weekDay = "星期一";
                    break;
                case DayOfWeek.Tuesday:
                    weekDay = "星期二";
                    break;
                case DayOfWeek.Wednesday:
                    weekDay = "星期三";
                    break;
                case DayOfWeek.Thursday:
                    weekDay = "星期四";
                    break;
                case DayOfWeek.Friday:
                    weekDay = "星期五";
                    break;
                case DayOfWeek.Saturday:
                    weekDay = "星期六";
                    break;
            }

            return weekDay;
        }

        private string GetChartSubTitle()
        {
            string title1 = string.Empty;

            var selectAreaExts = m_ViewModel.AreaExtends.Where(a => a.IsSelected).ToList();
            if (selectAreaExts.Count == 1)
            {
                title1 = selectAreaExts[0].Name;
            }
            else if (selectAreaExts.Count > 1)
            {
                title1 = "各店铺";
            }

            return title1;
        }

        private List<WeekComparePassengerFlow> GetWeekComparePassengerFlow()
        {
            List<WeekComparePassengerFlow> weeksComparePassengerFlows = new List<WeekComparePassengerFlow>();

            List<DayOfWeek> dayOfWeeks = new List<DayOfWeek>();
            DateTime time = new DateTime((int)cmbYear.SelectedValue, (int)cmbMonth.SelectedValue, 1, 0, 0, 0);
            DateTime endTime = time.AddMonths(1);

            dayOfWeeks.Clear();
            dayOfWeeks.Add(DayOfWeek.Monday);
            var monDaysource = m_ViewModel.GetHistoryPassengerByWeek(false, time, endTime, dayOfWeeks);

            dayOfWeeks.Clear();
            dayOfWeeks.Add(DayOfWeek.Tuesday);
            var tueDaysource = m_ViewModel.GetHistoryPassengerByWeek(false, time, endTime, dayOfWeeks);

            dayOfWeeks.Clear();
            dayOfWeeks.Add(DayOfWeek.Wednesday);
            var wedDaysource = m_ViewModel.GetHistoryPassengerByWeek(false, time, endTime, dayOfWeeks);

            dayOfWeeks.Clear();
            dayOfWeeks.Add(DayOfWeek.Thursday);
            var thuDaysource = m_ViewModel.GetHistoryPassengerByWeek(false, time, endTime, dayOfWeeks);

            dayOfWeeks.Clear();
            dayOfWeeks.Add(DayOfWeek.Friday);
            var friDaysource = m_ViewModel.GetHistoryPassengerByWeek(false, time, endTime, dayOfWeeks);

            dayOfWeeks.Clear();
            dayOfWeeks.Add(DayOfWeek.Saturday);
            var satDaysource = m_ViewModel.GetHistoryPassengerByWeek(false, time, endTime, dayOfWeeks);

            dayOfWeeks.Clear();
            dayOfWeeks.Add(DayOfWeek.Sunday);
            var sunDaysource = m_ViewModel.GetHistoryPassengerByWeek(false, time, endTime, dayOfWeeks);


            DateTime startTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
            endTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);

            decimal percent = 0;
            WeekComparePassengerFlow tempPassengerFlow = null;
            bool isIn = cmbInorOut.SelectedIndex == 0 ? true : false;
            int allInCount = monDaysource.Sum(s => s.InCount) + tueDaysource.Sum(s => s.InCount) + wedDaysource.Sum(s => s.InCount) +
                             thuDaysource.Sum(s => s.InCount) + friDaysource.Sum(s => s.InCount) + satDaysource.Sum(s => s.InCount) + sunDaysource.Sum(s => s.InCount);
            int allOutCount = monDaysource.Sum(s => s.OutCount) + tueDaysource.Sum(s => s.OutCount) + wedDaysource.Sum(s => s.OutCount) +
                             thuDaysource.Sum(s => s.OutCount) + friDaysource.Sum(s => s.OutCount) + satDaysource.Sum(s => s.OutCount) + sunDaysource.Sum(s => s.OutCount);
            while (startTime < endTime)
            {
                tempPassengerFlow = new WeekComparePassengerFlow();
                tempPassengerFlow.HistoryTime = startTime;
                tempPassengerFlow.DisplayDate = string.Format("{0:t}", startTime);
                tempPassengerFlow.MonDayCount = isIn ? monDaysource.Where(m => m.HistoryTime == startTime).First().InCount :
                                                     monDaysource.Where(m => m.HistoryTime == startTime).First().OutCount;
                tempPassengerFlow.TueDayCount = isIn ? tueDaysource.Where(m => m.HistoryTime == startTime).First().InCount :
                                                     tueDaysource.Where(m => m.HistoryTime == startTime).First().OutCount;
                tempPassengerFlow.WedDayCount = isIn ? wedDaysource.Where(m => m.HistoryTime == startTime).First().InCount :
                                                      wedDaysource.Where(m => m.HistoryTime == startTime).First().OutCount;
                tempPassengerFlow.ThuDayCount = isIn ? thuDaysource.Where(m => m.HistoryTime == startTime).First().InCount :
                                                      thuDaysource.Where(m => m.HistoryTime == startTime).First().OutCount;
                tempPassengerFlow.FriDayCount = isIn ? friDaysource.Where(m => m.HistoryTime == startTime).First().InCount :
                                                      friDaysource.Where(m => m.HistoryTime == startTime).First().OutCount;
                tempPassengerFlow.SatDayCount = isIn ? satDaysource.Where(m => m.HistoryTime == startTime).First().InCount :
                                                      satDaysource.Where(m => m.HistoryTime == startTime).First().OutCount;
                tempPassengerFlow.SunDayCount = isIn ? sunDaysource.Where(m => m.HistoryTime == startTime).First().InCount :
                                                      sunDaysource.Where(m => m.HistoryTime == startTime).First().OutCount;
                tempPassengerFlow.WorkDayCount = tempPassengerFlow.MonDayCount + tempPassengerFlow.TueDayCount + tempPassengerFlow.WedDayCount +
                                                 tempPassengerFlow.ThuDayCount + tempPassengerFlow.FriDayCount;

                int allSecIn = isIn ? allInCount : allOutCount;
                percent = allSecIn > 0 ? ((decimal)tempPassengerFlow.WorkDayCount / allSecIn) * 100 : 0;
                percent = Math.Round(percent, 2);
                tempPassengerFlow.WorkDayPercent = percent > 0 ? percent.ToString() + "%" : string.Empty;

                tempPassengerFlow.HoliDayCount = tempPassengerFlow.SatDayCount + tempPassengerFlow.SunDayCount;

                percent = allSecIn > 0 ? ((decimal)tempPassengerFlow.HoliDayCount/ allSecIn) * 100 : 0;
                percent = Math.Round(percent, 2);
                tempPassengerFlow.HoliDayPercent = percent > 0 ? percent.ToString() + "%" : string.Empty;

                tempPassengerFlow.AllCount = tempPassengerFlow.MonDayCount + tempPassengerFlow.TueDayCount + tempPassengerFlow.WedDayCount +
                                                 tempPassengerFlow.ThuDayCount + tempPassengerFlow.FriDayCount + tempPassengerFlow.SatDayCount +
                                                 tempPassengerFlow.SunDayCount;

                percent = allSecIn > 0 ? ((decimal)tempPassengerFlow.AllCount / allSecIn) * 100 : 0;
                percent = Math.Round(percent, 2);
                tempPassengerFlow.AllPercent = percent > 0 ? percent.ToString() + "%" : string.Empty;

                weeksComparePassengerFlows.Add(tempPassengerFlow);
                startTime = startTime.AddMinutes(30);
            }

            tempPassengerFlow = new WeekComparePassengerFlow();
            tempPassengerFlow.DisplayDate = "总计";
            tempPassengerFlow.MonDayCount = isIn ? monDaysource.Sum(s => s.InCount) : monDaysource.Sum(s => s.OutCount);
            tempPassengerFlow.TueDayCount = isIn ? tueDaysource.Sum(s => s.InCount) : tueDaysource.Sum(s => s.OutCount);
            tempPassengerFlow.WedDayCount = isIn ? wedDaysource.Sum(s => s.InCount) : wedDaysource.Sum(s => s.OutCount);
            tempPassengerFlow.ThuDayCount = isIn ? thuDaysource.Sum(s => s.InCount) : thuDaysource.Sum(s => s.OutCount);
            tempPassengerFlow.FriDayCount = isIn ? friDaysource.Sum(s => s.InCount) : friDaysource.Sum(s => s.OutCount);
            tempPassengerFlow.SatDayCount = isIn ? satDaysource.Sum(s => s.InCount) : satDaysource.Sum(s => s.OutCount);
            tempPassengerFlow.SunDayCount = isIn ? sunDaysource.Sum(s => s.InCount) : sunDaysource.Sum(s => s.OutCount);

            tempPassengerFlow.WorkDayCount = tempPassengerFlow.MonDayCount + tempPassengerFlow.TueDayCount + tempPassengerFlow.WedDayCount +
                                                 tempPassengerFlow.ThuDayCount + tempPassengerFlow.FriDayCount;

            int allSecIn2 = isIn ? allInCount : allOutCount;
            percent = allSecIn2 > 0 ? ((decimal)tempPassengerFlow.WorkDayCount / allSecIn2) * 100 : 0;
            percent = Math.Round(percent, 2);
            tempPassengerFlow.WorkDayPercent = percent > 0 ? percent.ToString() + "%" : string.Empty;

            tempPassengerFlow.HoliDayCount = tempPassengerFlow.SatDayCount + tempPassengerFlow.SunDayCount;

            percent = allSecIn2 > 0 ? ((decimal)tempPassengerFlow.HoliDayCount / allSecIn2) * 100 : 0;
            percent = Math.Round(percent, 2);
            tempPassengerFlow.HoliDayPercent = percent > 0 ? percent.ToString() + "%" : string.Empty;

            tempPassengerFlow.AllCount = tempPassengerFlow.MonDayCount + tempPassengerFlow.TueDayCount + tempPassengerFlow.WedDayCount +
                                             tempPassengerFlow.ThuDayCount + tempPassengerFlow.FriDayCount + tempPassengerFlow.SatDayCount +
                                             tempPassengerFlow.SunDayCount;

            tempPassengerFlow.AllPercent = string.Empty;

            weeksComparePassengerFlows.Add(tempPassengerFlow);

            return weeksComparePassengerFlows;
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog fsd = new SaveFileDialog();
            fsd.Filter = "PDF (*.pdf)|*.pdf|*PNG (*.png)|*.png";

            if (fsd.ShowDialog() == true)
            {
                LocalBusyIndicator indicator = Application.Current.RootVisual as LocalBusyIndicator;
                indicator.IsBusy = true;
                this.Dispatcher.BeginInvoke(delegate()
                {
                    FayshineManager.Instance.ExportToDisk(fsd, spContent);
                });
            }
        }

    }
}
